package com.part2.common.tag;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.part2.common.query.Page;

/**
 * Pager Tag
 * TODO 
 * 
 * @author 
 *
 */
public class PagerTag extends TagSupport {
	
	private String action;
	public void setAction(String action) {
		this.action = action;
	}
	
	public int doStartTag() throws JspException {
		return EVAL_BODY_INCLUDE;
	}

	public int doEndTag() throws JspException {
		
		HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
		Page page= (Page)request.getAttribute("page");
		JspWriter out = pageContext.getOut();
		
		// 第?页 共?页 [上一页 下一页]
		
		StringBuffer sb= new StringBuffer();
		
		sb.append("<div class=\"pager\">");
		sb.append("第" + page.getPageNo() + "页");
		sb.append(" ");
		sb.append("共" + page.getTotalPages() + "页");
		sb.append(" ");
		sb.append("共" + page.getTotalCount()+ "条记录");
		
		sb.append(" [");
		if(page.isHasPre()){
			sb.append("<a href=\""+action +
					"&pageNo=" + page.getPrePage()+
					"&pageSize=" + page.getPageSize()+
					"&totalCount=" + page.getTotalCount()+
					"\">");
			sb.append("上一页");
			sb.append("</a>");
		}else{
			sb.append("上一页");
		}
		
		sb.append(" ");
		
		if(page.isHasNext()){			
			sb.append("<a href=\""+action +
					"&pageNo=" + page.getNextPage()+
					"&pageSize=" + page.getPageSize()+
					"&totalCount=" + page.getTotalCount()+
					"\">");
			sb.append("下一页");
			sb.append("</a>");
		}else{
			sb.append("下一页");
		}
		
		sb.append("]</div>");
		
		try {
			out.print(sb.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return EVAL_PAGE;
	}
}
